home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14582 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: news.mcs.net!usenet
  2. From: Mike Young <mikey@mcs.com>
  3. Newsgroups: comp.lang.ada,comp.lang.c++
  4. Subject: Re: some questions re. Ada/GNAT from a C++/GCC user
  5. Date: Sun, 31 Mar 1996 21:16:45 -0600
  6. Organization: Fen Software, Inc.
  7. Message-ID: <315F4A9D.7E6F@mcs.com>
  8. References: <wnewmanDoxrCp.DKv@netcom.com> <SIMON.96Mar30153124@pogner.demon.co.uk> <315D902C.6F7B@escmail.orl.mmc.com> <Dp3G4u.KEA@world.std.com> <4jmuj5$lkh@dayuc.dayton.saic.com>
  9. NNTP-Posting-Host: mikey.pr.mcs.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  14.  
  15. John G. Volan wrote:
  16. > One difficulty I see with intermingling declarations and statements is
  17. > how to interpret declarations within conditional or iterative constructs.
  18. > For instance:
  19. >     -- This is NOT Ada, this is CRAPOLA (C-Reminiscent Ada-like Perversion
  20. >     -- Of Language Aspects)  :-) :
  21. >     begin
  22. >         ...
  23. >         if Smaller then
  24. >             X : Integer;
  25. >             ...
  26. >         elsif Bigger then
  27. >             X : Long_Integer;
  28. >             ...
  29. >         end if;
  30. >         ...
  31. >         -- is X in scope here, and if so, what the heck is it?
  32. >         ...
  33.  
  34. ==========
  35. (Cute acronym; not sure if I like it though. :)
  36.  
  37. The scoping rules in C++ are quite simple: the object comes into 
  38. existence at the point of definition, and persists until the end of the 
  39. enclosing scope. In your example, X would is created at its declaration 
  40. (eg.: X : Long_Integer;), and is destroyed at the corresponding elsif or 
  41. endif.
  42.  
  43. Sprinkling declarations willy-nilly throughout the code is more than a 
  44. matter of convenience. Construction of objects sometimes entail 
  45. non-trivial construction. Rather than force a two step process -- 
  46. construction at declaration, and then subsequent initialization -- it is 
  47. very common to declare the object "just-in-time," and combine the two 
  48. steps. This frees the object designer from allowing for an undefined 
  49. state, which would exist if construction were separated from 
  50. initialization.
  51.  
  52. Personally, I like it this way. It's very easy to verify that all 
  53. objects are correctly initialized before use when all objects are 
  54. initialized at their points of construction. The object does not exist 
  55. before it is initialized.
  56.  
  57. Mike.
  58.